package ch.sfug.sequence {
	import flash.events.Event;
	import flash.events.EventDispatcher;		

	/**
	 * @author Administrator
	 */
	public class EventAwait extends EventDispatcher implements ISequenceChild {

		private var ed:EventDispatcher;
		private var t:String;
		private var call:FunctionCall;

		public function EventAwait( dispatcher:EventDispatcher, eventType:String, funcCall:FunctionCall ) {
			super();
			ed = dispatcher;
			t = eventType;
			call = funcCall;
		}
		
		
		public function hasToWait():Boolean {
			return true;
		}
		
		public function execute():void {
			ed.addEventListener( t, delegate);
			call.execute();
		}
		
		private function delegate( event:String ):void {
			ed.removeEventListener( t, delegate );
			dispatchEvent( new Event( Event.COMPLETE ) );
		}

		public function stop():void {
			ed.removeEventListener( t, delegate );
		}
		
		public override function toString() : String {
			return "EventAwait listen on: " + t + " of " + ed + " Object after call:" + call;
		}
		
		public function setArguments(args:Array):void {
			call.setArguments( args );
		}
	}
}
